﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using ERP.ERPSystem.ProductionSystem;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using Microsoft.AspNetCore.Mvc;
using ERP.Common.Results;
using Microsoft.AspNetCore.Cors;
using Volo.Abp.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using System;

namespace ERP.Web.Controllers.ProductionSystem
{
    public class ProductionWorkOrderMainController : AbpController
    {
        private readonly IProductionWorkOrderMainServices _productionWorkOrderMainServices;

        public ProductionWorkOrderMainController(IProductionWorkOrderMainServices productionWorkOrderMainServices)
        {
            _productionWorkOrderMainServices = productionWorkOrderMainServices;
        }

        /// <summary>
        /// 生产工单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult GetProduction()
        {
            return View();
        }

        /// <summary>
        /// 保存工单
        /// </summary>
        /// <param name="model">工单数据</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> AddProductionWorkOrder([FromBody] ProductionWorkOrderDto model)
        {
            try
            {
                if (model == null)
                {
                    return Json(new { success = false, message = "数据不能为空" });
                }

                var result = await _productionWorkOrderMainServices.AddProductionWorkOrder(model);
                return Json(new { success = true, message = "保存成功", data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 获取生产工单列表（支持分页和搜索）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetProductionWorkOrderList(string workOrderCode = "", string workOrderName = "", string productCode = "")
        {
            try
            {
                var list = await _productionWorkOrderMainServices.GetProductionWorkOrder(
                    workOrderCode: workOrderCode,
                    workOrderName: workOrderName,
                    productCode: productCode
                );

                return Json(new { code = 0, msg = "", count = list.Count, data = list });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = ex.Message });
            }
        }

        /// <summary>
        /// 添加生产工单页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddProductionWorkOrderMain()
        {
            return View();
        }

        /// <summary>
        /// 删除生产工单
        /// </summary>
        /// <param name="id">工单ID</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> DeleteWorkOrder(int id)
        {
            try
            {
                await _productionWorkOrderMainServices.DeleteProductionWorkOrder(id);
                return Json(new { success = true, message = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 根据ID获取工单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetWorkOrderById(int id)
        {
            try
            {
                var result = await _productionWorkOrderMainServices.GetAsync(id);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 编辑工单页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult EditWorkOrder(int id)
        {
            return View();
        }

        /// <summary>
        /// 更新工单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> UpdateWorkOrder([FromBody] ProductionWorkOrderMain model)
        {
            try
            {
                var result = await _productionWorkOrderMainServices.UpdateWorkOrder(model);
                return Json(new { success = result, message = result ? "更新成功" : "更新失败" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }
    }
}
